/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "remote_dir.h"
#include "remote_file.h"
#include "list.h"
static void on_changed(GtkWidget *widget, gpointer label);
static gint double_click(GtkWidget *widget, GdkEvent *event);
static char s_current_dir[255] = { '\0' };
static OnListSelected on_select = &on_changed;
static OnDoubleClick on_double_click = &double_click;
static GtkWidget *s_dir_view;
static int fd_socket = 0;



void list_dir_hander(char* buffer) {
	clean_list(s_dir_view);
	clean_all_fines();
	char* part = strtok(buffer, "\n");
	while (part != NULL) {
		char dirname[256] = { '\0' };
		strcpy(dirname, substr(part, 55, strlen(part) - 56));
		if (strlen(part) > 10 && 'd' == part[0]) {
			add_to_list(s_dir_view, g_locale_to_utf8(dirname, -1, NULL, NULL, NULL));
		}else if(strlen(part) > 10 && '-' == part[0]){
			add_one_file(g_locale_to_utf8(dirname, -1, NULL, NULL, NULL));
		}
		part = strtok(NULL, "\n");
	}
	//reshow_view();
}

static void init_remote_files(GtkWidget * window) {
	s_dir_view = gtk_tree_view_new();
	init_list(s_dir_view);
	init_list_view(window, s_dir_view, on_select, on_double_click);
	char cwd[255];
	getcwd(cwd, 255);
	strcpy(s_current_dir, cwd);
}

static void on_changed(GtkWidget *widget, gpointer label) {
	/*
	GtkTreeIter iter;
	GtkTreeModel *model;
	if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
		char *value;
		gtk_tree_model_get(model, &iter, 0, &value, -1);
		char real_path[255] = { '\0' };
		sprintf(real_path, "%s/%s", s_current_dir, value);
		char* real_path1 = realpath(real_path, NULL);
		g_print(" on changed %s\n", real_path1);
		//padding_remote_filenname(real_path1);
		g_free(value);
	}
	*/
}

static void padding_dirname(char* cwd) {
	clean_list(s_dir_view);
	DIR *pdir = NULL;
	if ((pdir = opendir(cwd)) == NULL) {
		printf("open dir error! %s \n", cwd);
	} else {
		struct dirent* entry;
		while (entry = readdir(pdir)) {
			if (DT_DIR == entry->d_type) {
				add_to_list(s_dir_view, entry->d_name);
			}
		}
		closedir(pdir);
	}
}
static gint double_click(GtkWidget *widget, GdkEvent *event) {
	GdkEventButton *bevent = (GdkEventButton *) event;
	if (event->type == GDK_2BUTTON_PRESS) {
		GtkTreeIter iter;
		GtkTreeModel *model;
		if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
			char *value;
			gtk_tree_model_get(model, &iter, 0, &value, -1);
			trim(value);
			//CWD degui

			char buffer[1024];

			char cwd1[] = "PWD\r\n";
			send_cmd(fd_socket, cwd1);
			recv_cmd(fd_socket, buffer, 1024);
			substring(buffer, "\"", "\"", s_current_dir);
			printf("PWD result : %s \n", s_current_dir);

			char remote_path[255] = { '\0' };
			sprintf(remote_path, "%s/%s", s_current_dir, value);
			g_print(" on double_click %s\n", remote_path);

			char cwd[256] = {'\0'};
			sprintf(cwd, "CWD %s\r\n", remote_path);
			send_cmd(fd_socket, cwd);
			recv_cmd(fd_socket, buffer, 1024);
			//recv_cmd(fd_socket, buffer);
			printf("commond result : %s", buffer);


			char list[] = "LIST\r\n";
			list_comment(fd_socket, list);
			g_free(value);
		}
		return TRUE;
	}
	return FALSE;
}


void init_remote_dir(int fd) {
	fd_socket = fd;
	char list[] = "LIST\r\n";
	list_comment_hander hander = &list_dir_hander;
	set_list_result_hander(hander);
	list_comment(fd, list);

	char buffer[1024];
	char cwd1[] = "PWD\r\n";
	send_cmd(fd, cwd1);
	recv_cmd(fd, buffer, 1024);
	substring(buffer, "\"", "\"", s_current_dir);
	printf("PWD result : %s \n", s_current_dir);
}

static void remote_dir_tree(GtkWidget *paned) {
	GtkWidget *vbox = gtk_vbox_new(FALSE, 1);
	gtk_paned_add1(GTK_PANED(paned), vbox);
	GtkWidget *valign = gtk_alignment_new(1, 0, 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), valign, FALSE, FALSE, 0);

	init_remote_files(vbox);
	//padding_dirname(g_current_dir);

}

void remote(GtkWidget *paned) {
	GtkWidget *paned1 = gtk_vpaned_new();/*创建纵向分栏窗口构件*/

	gtk_container_add(GTK_CONTAINER(paned), paned1);/*将分栏窗口构件加入窗体*/
	gtk_widget_show(paned1);
	remote_dir_tree(paned1);
	remote_dir_file(paned1);
}

void refresh_remote_dir(){
	char list[] = "LIST\r\n";
	list_comment(fd_socket, list);
}
